﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using XingFuStudio.Pioneer.Applications.City.Entity;

namespace XingFuStudio.Pioneer.Applications.City.Element {
    /// <summary>
    /// 元素
    /// </summary>
    public class CityElement {
        /// <summary>
        /// 元素ID
        /// </summary>
        private int elementId = 0;
        /// <summary>
        /// 元素名称
        /// </summary>
        private string elementName = String.Empty;
        /// <summary>
        /// 元素关键字
        /// </summary>
        private string key = String.Empty;
        /// <summary>
        /// 元素大小
        /// </summary>
        private Size size = new Size(0, 0);
        /// <summary>
        /// 元素放置的位置坐标
        /// </summary>
        private Point location = new Point(0, 0);
        /// <summary>
        /// 元素分类
        /// </summary>
        private string elementClass = String.Empty;
        /// <summary>
        /// 增益
        /// </summary>
        private int bonus = 0;
        /// <summary>
        /// 租金收益
        /// </summary>
        private int rent = 0;
        /// <summary>
        /// 定单编号
        /// </summary>
        private string orderId = String.Empty;

        /// <summary>
        /// 获取或设置定单编号
        /// </summary>
        public string OrderId {
            get {
                return orderId;
            }
            set {
                orderId = value;
            }
        }

        /// <summary>
        /// 获取或设置租金收益
        /// </summary>
        public int Rent {
            get {
                return rent;
            }
            set {
                rent = value;
            }
        }

        /// <summary>
        /// 获取或设置增益
        /// </summary>
        public int Bonus {
            get {
                return bonus;
            }
            set {
                bonus = value;
            }
        }

        /// <summary>
        /// 获取或设置元素分类
        /// </summary>
        public string ElementClass {
            get {
                return elementClass;
            }
            set {
                elementClass = value;
            }
        }

        /// <summary>
        /// 获取或设置元素放置的位置坐标
        /// </summary>
        public Point Location {
            get {
                return location;
            }
            set {
                location = value;
            }
        }

        /// <summary>
        /// 获取或设置元素大小
        /// </summary>
        public Size Size {
            get {
                return size;
            }
            set {
                size = value;
            }
        }

        /// <summary>
        /// 获取或设置元素ID
        /// </summary>
        public int ElementId {
            get {
                return elementId;
            }
            set {
                elementId = value;
            }
        }

        /// <summary>
        /// 获取或设置元素名称
        /// </summary>
        public string ElementName {
            get {
                return elementName;
            }
            set {
                elementName = value;
            }
        }

        /// <summary>
        /// 初始化元素自身数据
        /// </summary>
        /// <param name="elementObject"></param>
        public virtual void InitElement(Dictionary<string, object> elementObject) {
            //赋值属性
            this.ElementId = Convert.ToInt32((string)elementObject["eid"]);
            this.ElementClass = (string)elementObject["classname"];
            this.OrderId = (string)elementObject["orderid"];
            this.Location = new System.Drawing.Point(
                Convert.ToInt32((string)elementObject["x"]), Convert.ToInt32((string)elementObject["y"]));
        }


        /// <summary>
        /// 是否可以升级
        /// </summary>
        /// <param name="upLevelConfigs"></param>
        /// <returns></returns>
        public bool CanUpLevel(List<UpLevelConfigInfo> upLevelConfigs, List<UpLevelElementInfo> upLevelData) {
            var element = upLevelData.Where(s => s.TargetOrderId == this.OrderId);

            if (element.Count() > 0) {
                var item = upLevelConfigs.Where(s => s.ElementId == element.First().InitiElementId);

                if (item.Count() > 0) {
                    var level = item.First().LevelConfigs
                        .Where(s => s.Level == element.First().Level && s.Level != item.First().LevelConfigs.Max(f => f.Level));

                    if (level.Count() > 0) {
                        if (level.First().Number <= element.First().Total) {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
    }
}
